package supermarket.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import supermarket.dao.CustomerDao;
import supermarket.model.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Override
	public Customer findCustomer(Integer id) {
		return (Customer)getHibernateTemplate().get(Customer.class, id);
	}
	
	@Override
	public List findCustomerByName(String name) {
		List customerList = this.getHibernateTemplate().find(
			"FROM Customer AS customer WHERE customer.name LIKE ?", name + "%");
		if(customerList.isEmpty()) {
			return null;
		}
		return customerList;
	}
	
	@Override
	public List findAll() {
		return (List)getHibernateTemplate().find("FROM Customer");
	}
	
	@Override
	public void addCustomer(Customer customer) {
		getHibernateTemplate().save(customer);		
	}
	
	@Override
	public void addCustomerByName(String name) {
		getHibernateTemplate().save(new Customer(name, name));
	}
	
	@Override
	public void updateCustomer(Customer customer) {
		getHibernateTemplate().update(customer);
	}
	
	@Override
	public void removeCustomer(Customer customer) {
		getHibernateTemplate().delete(customer);
	}

}
